package com.marketlive.system.config;

//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;

/*
(C) Copyright MarketLive. 2006. All rights reserved.
MarketLive is a trademark of MarketLive, Inc.
Warning: This computer program is protected by copyright law and international treaties.
Unauthorized reproduction or distribution of this program, or any portion of it, may result
in severe civil and criminal penalties, and will be prosecuted to the maximum extent
possible under the law.
*/

import org.marketlive.system.config.IConfigurationNode;
import org.marketlive.system.config.IConfigurationListener;

import java.util.ArrayList;

/**
 * A point in the configuration tree.
 */
public abstract class ConfigurationNode implements IConfigurationNode {

    private String name;
    private ArrayList listeners = new ArrayList();

    /**
     * Logger
     */
    //private static Log log = LogFactory.getLog(ConfigurationNode.class);

    public ConfigurationNode(String name) {
    	if (null == name || name.equals("")) {
    		throw new IllegalArgumentException("node name cannot be null or the null String: '" + name + "'");
    	}
    	else if (-1 != name.indexOf(".")) {
    		throw new IllegalArgumentException("Periods not allowed in node name: " + name);
    	}
        this.name = name;
    }
    
    /**
     * @inheritDoc
     */
    public String getName() {
        return name;
    }

    /**
     * @inheritDoc 
     */
    public void listen(IConfigurationListener listener) {
        listeners.add(listener);
    }

    /**
     * Returns a formatted string.
     * @param path the <code>String</code> array to use as the source 
     * @return the formatted <code>String</code>
     */
    protected static String arrayToString(final String[] path) {
        StringBuffer sb = new StringBuffer(32);
        sb.append("[");
        for (int i=0;i<path.length;i++) {
            sb.append(path[i]);
            if (i!=path.length-1) {
                sb.append('.');
            }
        }
        sb.append("]");
        return sb.toString();
    }

}
